package com.qf2.controller;

import com.qf2.consts.Code;
import com.qf2.domain.Order;
import com.qf2.domain.User;
import com.qf2.exhandlers.BussinessException;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/test1")
    @ResponseBody
    public String test1(User user, @RequestBody Order order){
//      返回String，内部使用的消息转换器是： StringHttpMessageConverter
        System.out.println("user:" + user);
        System.out.println("order:" + order);

        return "成功~~";
    }

    @RequestMapping("/test2")
    @ResponseBody
    public User test2(){
//      返回的是java对象，内部使用的消息转换器是：  MappingJackson2HttpMessageConverter，这个对象内部使用的Jackson将对象转成Json格式字符串
        User user = new User();
        user.setAge(25);
        user.setName("李四");
        user.setBir(new Date());

        int i =1/0;

        return user;
    }

    @RequestMapping("/test3")
    @ResponseBody
    @CrossOrigin(origins = "*")
    public List<Order> test3(){
        List<Order> list = new ArrayList();
        Order order = new Order();
        order.setId(1);
        order.setPrice(200d);
        order.setDesc("外卖1");
        list.add(order);

        order = new Order();
        order.setId(2);
        order.setPrice(400d);
        order.setDesc("外卖2");
        list.add(order);

        return list;
    }

    @RequestMapping("/login")
    @ResponseBody
    public String login(@RequestParam("uname") String uname,
                        @RequestParam("pwd") String pwd,
                        @RequestParam("age") Integer age,
                        HttpServletRequest req){

        if (age < 0){
            throw new BussinessException(Code.AGE_LT_0);
        }

        if (age > 120){
            throw new BussinessException(Code.AGE_GT_120);
        }

        if ("zs".equals(uname) && "123".equals(pwd)){
            //登录成功
            //将当前登录用户的信息存入session中
            req.getSession().setAttribute("userInfo",uname);

            return "login success...";
        }

        throw new BussinessException(Code.USERNAME_OR_PWD_ERROR);
    }

    @RequestMapping("/upload1")
    @ResponseBody
    //<input type="file" name="file1" > <br>
    public String upload(MultipartFile file1,MultipartFile file2,@RequestParam("name") String name2) throws IOException {

        String name = file1.getName();
        System.out.println("name:" + name);
        String filename = file1.getOriginalFilename();
        System.out.println("filename:" + filename); // b.abc.jpg
        long size = file1.getSize();
        System.out.println("size:" + size);

        //保存文件
        //解决文件重名问题(生成唯一文件名-uuid)c  [目录分离--把我们的文件数据分开不同的目录存储，防止一个目录下的文件过多导致的读写效率问题]
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String suffix = filename.substring(filename.lastIndexOf("."));
        filename = uuid + suffix;

        File destFile = new File("E:/" + filename);
        file1.transferTo(destFile);

        String file2name = file2.getOriginalFilename();
        uuid = UUID.randomUUID().toString().replaceAll("-", "");
        suffix = file2name.substring(file2name.lastIndexOf("."));
        file2name = uuid + suffix;
        destFile = new File("E:/" + file2name);
        file2.transferTo(destFile);

        System.out.println("====" + name2);

        return "上传成功...";
    }
}
